package com.pengtu.gsj.service.impl.afgc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.pengtu.gsj.dao.afgc.LifeCherishDao;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.afgc.LifeCherish;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.base.LabelValue;
import com.pengtu.gsj.service.afgc.LifeCherishService;
import com.pengtu.gsj.utils.ConfigUtils;
import com.pengtu.gsj.utils.ExcelImportUtils;
import com.pengtu.gsj.utils.FileUtils;
import com.pengtu.gsj.utils.LabelHelper;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.ZipUtils;

@Transactional
@Service
public class LifeCherishServiceImpl implements LifeCherishService {
	
	@Autowired
	private LifeCherishDao lifeCherishDao;

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> importHigherProject(
			MultipartFile multipartFile, String type) throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map;
		//判断文件是否为空
		if (multipartFile == null || multipartFile.getSize() == 0) {
			map = new HashMap<String, Object>();
			map.put("error", "文件为空");
			list.add(map);
			return list;
		}
		
		list = ExcelImportUtils.getExcelContent(multipartFile, type);
		if (list.size()>=2) {
			Boolean flag = (Boolean) list.get(0).get("result");
			if (flag) {
				Map<String, Object> projectMap = list.get(2);
				List<LifeCherish> lifeCherishs = (List<LifeCherish>) projectMap.get("projectInfo");
				for (LifeCherish lifeCherish : lifeCherishs) {
					//保存数据库
					lifeCherishDao.save(lifeCherish);
				}
			}
		}
		return list;
	}

	@Override
	public Page<LifeCherish> findLifeCherish(Page<LifeCherish> page,
			QueryFilter queryFilter) {
		return lifeCherishDao.findPage(page, queryFilter);
	}

	@Override
	public LifeCherish getLifeCherishById(String id) {
		return lifeCherishDao.get(id);
	}

	@Override
	public void saveLifeCherish(LifeCherish lifeCherish) {
		lifeCherishDao.save(lifeCherish);
	}

	@Override
	public List<LifeCherish> getLifeCherishs(QueryFilter queryFilter) {
		return lifeCherishDao.find(queryFilter);
	}

	@Override
	public List<String> getLifeCherishsYears() {
		List<String> list = new ArrayList<String>();
		List<LifeCherish> lifeCherishs = lifeCherishDao.getLifeCherishsYears();
		if (lifeCherishs != null) {
			for (LifeCherish lifeCherish : lifeCherishs) {
				list.add(lifeCherish.getYear());
			}
		}
		return list;
	}

	/**
	 * 
	 * getLifeCherishsByAll:
	 * 适用:获取某年度各辖区局 各种技术等级  各种处置状态下的工程数量
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<LifeCherish> getLifeCherishsByAll(String year) {
		return lifeCherishDao.getLifeCherishsByAll(year);
	}

	/**
	 * 
	 * getlifCherishsByCounty:
	 * 适用:获取某年度各辖区局下面需要处置的工程总数量 部分技术等级和处置状态
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<LifeCherish> getlifCherishsByCounty(String year) {
		return lifeCherishDao.getlifCherishsByCounty(year);
	}

	/**
	 * 
	 * getLifeCherishsByTec:
	 * 适用:获得某年度各辖区局下面各技术等级的工程数量
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<LifeCherish> getLifeCherishsByTec(String year) {
		return lifeCherishDao.getLifeCherishsByTec(year);
	}

	/**
	 * 
	 * getLifeCherishsByDelStatus:
	 * 适用:获得某年度各辖区局下面各处置状态的工程数量
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<LifeCherish> getLifeCherishsByDelStatus(String year) {
		return lifeCherishDao.getLifeCherishsByDelStatus(year);
	}

	@Override
	public List<Map<String, Object>> getCountJson(String year) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = null;
		if (StringUtils.isEmpty(year)) {
			return list;
		}
		List<LifeCherish> lifeCherishsTec = lifeCherishDao.getLifeCherishsByTec(year);
 		List<LifeCherish> lifeCherishsDel = lifeCherishDao.getLifeCherishsByDelStatus(year);
 		List<LifeCherish> lifeCherishsCou = lifeCherishDao.getlifCherishsByCounty(year); 
 		//所有可能的处置状态和技术等级值
 		List<LabelValue> delStatus = LabelHelper.getInstance().buildCodeNames(6005);
 		List<LabelValue> techLevels = LabelHelper.getInstance().buildCodeNames(6000);
 		long acceptance = 0; //已验收数量
 		//添加各辖区局的汇总数据
 		for (LifeCherish lifeCherish : lifeCherishsCou) {
			//添加各辖区局下面对应个技术等级的工程数量
			for (LabelValue labelValue : techLevels) {
				map = new HashMap<String, Object>();
				map.put("name", LabelHelper.getInstance().buildCodeName(2052, lifeCherish.getCounty()));
				map.put("group", labelValue.getLabel());
				map.put("value", 0);
				Iterator<LifeCherish> iterator = lifeCherishsTec.iterator();
				while (iterator.hasNext()) {
					LifeCherish lifeCherishLevel = (LifeCherish) iterator.next();
					if (lifeCherish.getCounty().equals(lifeCherishLevel.getCounty()) && labelValue.getValue().equals(lifeCherishLevel.getTechnicalLevel())) {
						map.put("value", lifeCherishLevel.getCount());
						//将该项目从当前的list集合中移除  减少循环的次数
						iterator.remove();
						break;
					}
				}
				list.add(map);
			}
			
 			map = new HashMap<String, Object>();
			map.put("name", LabelHelper.getInstance().buildCodeName(2052, lifeCherish.getCounty()));
			map.put("group", "总数");
			map.put("value", lifeCherish.getCount());
			list.add(map);
			
			//添加各辖区局下面对应工程处置状态的工程数量
			for (LabelValue labelValue : delStatus) {
				map = new HashMap<String, Object>();
				map.put("name", LabelHelper.getInstance().buildCodeName(2052, lifeCherish.getCounty()));
				map.put("group", labelValue.getLabel());
				map.put("value", 0); //默认的状态值工程数量为0  当该辖区局下有该状态值的工程时   数量会被重新覆盖
				Iterator<LifeCherish> iterator = lifeCherishsDel.iterator();
				while (iterator.hasNext()) {
					LifeCherish lifeCherishStatus = (LifeCherish) iterator.next();
					if (lifeCherish.getCounty().equals(lifeCherishStatus.getCounty()) && labelValue.getValue().equals(lifeCherishStatus.getDelStatus())) {
						//得到已验收的工程数量  默认是0
						if(labelValue.getValue().equals("2")){
							acceptance = lifeCherishStatus.getCount();
						}
						map.put("value", lifeCherishStatus.getCount());
						//将该项目从当前的list集合中移除  减少循环的次数
						iterator.remove();
						break;
					}
				}
				list.add(map);
			}
			
			//添加已验收项目的验收率
			map = new HashMap<String, Object>();
			map.put("name", LabelHelper.getInstance().buildCodeName(2052, lifeCherish.getCounty()));
			map.put("group", "验收率(%)");
			map.put("value", MathsUtils.divDouble(acceptance*100, lifeCherish.getCount(), 2));
			list.add(map);
		}
		return list;
	}

	@Override
	public List<LifeCherish> getAllYears() {
		return lifeCherishDao.getAll("year", false);
	}

	@Override
	public ResponseEntity<byte[]> exportData(String year) throws Exception {
		if (StringUtils.isEmpty(year)) {
			return null;
		}
		List<LifeCherish> lifeCherishsTec = lifeCherishDao.getLifeCherishsByTec(year);
 		List<LifeCherish> lifeCherishsDel = lifeCherishDao.getLifeCherishsByDelStatus(year);
 		List<LifeCherish> lifeCherishsCou = lifeCherishDao.getlifCherishsByCounty(year); 
 		//所有可能的处置状态和技术等级值
 		List<LabelValue> delStatus = LabelHelper.getInstance().buildCodeNames(6005);
 		List<LabelValue> techLevels = LabelHelper.getInstance().buildCodeNames(6000);
 		long acceptance = 0; //已验收数量
 		List<List<String>> list = new ArrayList<List<String>>();
 		//添加各辖区局的汇总数据
 		for (LifeCherish lifeCherish : lifeCherishsCou) {
 			List<String> data = new ArrayList<String>();
			//添加各辖区局下面对应个技术等级的工程数量
 			data.add(LabelHelper.getInstance().buildCodeName(2052, lifeCherish.getCounty()));
			for (LabelValue labelValue : techLevels) {
				long count = 0;
				Iterator<LifeCherish> iterator = lifeCherishsTec.iterator();
				while (iterator.hasNext()) {
					LifeCherish lifeCherishLevel = (LifeCherish) iterator.next();
					if (lifeCherish.getCounty().equals(lifeCherishLevel.getCounty()) && labelValue.getValue().equals(lifeCherishLevel.getTechnicalLevel())) {
						count = lifeCherishLevel.getCount();
						//将该项目从当前的list集合中移除  减少循环的次数
						iterator.remove();
						break;
					}
				}
				data.add(String.valueOf(count));
			}
			
			data.add(String.valueOf(lifeCherish.getCount()));
			//添加各辖区局下面对应工程处置状态的工程数量
			for (LabelValue labelValue : delStatus) {
				long count = 0;
				Iterator<LifeCherish> iterator = lifeCherishsDel.iterator();
				while (iterator.hasNext()) {
					LifeCherish lifeCherishStatus = (LifeCherish) iterator.next();
					if (lifeCherish.getCounty().equals(lifeCherishStatus.getCounty()) && labelValue.getValue().equals(lifeCherishStatus.getDelStatus())) {
						//得到已验收的工程数量  默认是0
						if(labelValue.getValue().equals("2")){
							acceptance = lifeCherishStatus.getCount();
						}
						count = lifeCherishStatus.getCount();
						//将该项目从当前的list集合中移除  减少循环的次数
						iterator.remove();
						break;
					}
				}
				data.add(String.valueOf(count));
			}
			data.add(String.valueOf(MathsUtils.divDouble(acceptance*100, lifeCherish.getCount(), 2)));
			list.add(data);
		}
 		return ExcelImportUtils.insertDataToExcel("lifeProject.xls", list, "安防工程汇总表.xls");
	}

	@Override
	public ResponseEntity<byte[]> downAllFiles(LifeCherish lifeCherish) throws IOException {
		if (lifeCherish == null || lifeCherish.getAttachments().size() <= 0) {
			return null;
		}
		List<Attachment> attachments = lifeCherish.getAttachments();
		String rootPath = ConfigUtils.getConfig("lifeProject");
		String systemPath = System.getProperty("java.io.tmpdir")+"zipTemp\\"+System.currentTimeMillis()+"\\";
		String copyFilePath = systemPath+"相关文件\\";
		String lastZipDestPath = systemPath+"安防工程相关文件.zip";
		//如果文件目录不存在就创建
		FileUtils.createFolder(copyFilePath);
		for (Attachment attachment : attachments) {
			FileUtils.copy(rootPath+attachment.getFullPath(), copyFilePath, false);
		}
		ZipUtils.zip(copyFilePath, lastZipDestPath, true, null);
		FileUtils.deleteDirectory(copyFilePath, true);
		return FileUtils.downFile(lastZipDestPath, "安防工程相关文件.zip");
	}

}
